08 / 11

You need to cache data. Some data expires in 1 minute, some in 1 hour. How do you design this using OOP?

Difficulty: 3/10

Policy-Based Cache Design

I would hide the caching implementation behind an ICache abstraction and represent expiration as a cache policy rather than scattering TTL values throughout the application. Different consumers can provide different policies. The cache implementation can then enforce absolute or sliding expiration consistently.

javascript
  1. 1

    Separate cache abstraction from cache implementation.

  2. 2

    Represent TTL and other behavior as explicit policies.

  3. 3

    Support absolute and, where appropriate, sliding expiration.

  4. 4

    Define cache invalidation rules and consistency expectations.

  5. 5

    For distributed applications, consider a distributed cache rather than process-local memory.

  6. 6

    Monitor hit rate, eviction rate, memory usage, and latency.

Follow-up Questions

  • What is cache-aside?
  • How would you invalidate cached data after an update?
  • When would you use distributed caching?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.